package com.hsd.znsh.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.service.RedisService;
import com.lly835.bestpay.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-28 15:17:49
 */
@Service
public class RedisServiceImpl implements RedisService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 一周有多少秒
     */
    private static final long DAY_SECONDS =  24 * 60 * 60;

    /**
     * 将 key，value 存放到redis数据库中，默认设置过期时间为一周
     *
     * @param key
     * @param value
     */
    public void set(String key, Object value)  {
        try {
            redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(value), DAY_SECONDS, TimeUnit.SECONDS);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将 key，value 存放到redis数据库中，设置过期时间单位是秒
     *
     * @param key
     * @param value
     * @param expireTime
     */
    public void set(String key, Object value, long expireTime){
        try {
            redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(value), expireTime, TimeUnit.SECONDS);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断 key 是否在 redis 数据库中
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 获取与 key 对应的对象
     * @param key
     * @param clazz 目标对象类型
     * @param <T>
     * @return
     */
    public <T> T get(String key, Class<T> clazz){
        String s = get(key);
        if (s == null) return null;
        try {
            return objectMapper.readValue(s,clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取 key 对应的字符串
     * @param key
     * @return
     */
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除 key 对应的 value
     * @param key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        String string="{\"id\":7,\"username\":\"wangjc\",\"password\":\"$2a$10$sHIL8Mtc3i0ocRnmpVm0UeMVocW4ROQpqnDBo7LG2F0u7rdqmhb4y\",\"phone\":\"15270292627\",\"userType\":4,\"createDateTime\":1526287430000,\"updateDateTime\":1527577051000,\"createUserId\":5,\"deleted\":0,\"hikpassword\":null,\"hikusername\":null}";
        User user=objectMapper.readValue(string,User.class);
        System.out.println(user);
    }

}
